/*
 * Copyright (C) 2010, Funcom AS, All Rights Reserved
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  @authors
 *  Svein Gunnar Larsen <sveingunnarlarsen@gmail.com>
 *  Erik Thune Lund <culex.eriklund@gmail.com>
 *  Michel Krohn-Dale <kronicz@gmail.com>
 */

package com.funcom.peeler.peelable;

import com.funcom.peeler.NewAbsoluteLayout;
import com.funcom.peeler.component.PeelComponentFactory;
import com.funcom.peeler.symbol.Symbol;
import com.jmex.bui.BContainer;
import com.jmex.bui.layout.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.util.*;

public class PeelContainer extends PeelComponent {

    ArrayList<PeelComponent> children = new ArrayList<PeelComponent>();

    public PeelContainer() {
        this( new BContainer() );
    }

    public PeelContainer(String name)  {
        this( new BContainer() );
        component.setName(name);
    }

    protected PeelContainer( BContainer container ) {
        super( container );
        element_name = Symbol.Node.container;
    }

    public void setRoot( PeelRoot root ) {
        super.setRoot( root );

        for ( PeelComponent child : children ) {
            child.setRoot( getRoot() );
        }
    }

    /* PeelContainers can have children and must peel these too */
    public Element peel( Document doc ) {

        Element peeled_element = createPeeledInfo( doc );

        for ( PeelComponent peelable : children ) {

            if ( peelable != null ) {
                Element peeled_component = peelable.peel( doc );
                peeled_element.appendChild( peeled_component );
            }
        }
        return peeled_element;
    }

    /* PeelContainers can have children and must unpeel these too */
    public void unpeel( Element element ) {

        unpeelInfo( element );

        NodeList nodes = element.getChildNodes();

        for ( int i = 0 ; i < nodes.getLength() ; i++ ) {

            Node node = nodes.item(i);

            if ( node instanceof Element ) {

                PeelComponent peelComponent = PeelComponentFactory.makePeelComponent( (Element)node );

                if ( peelComponent != null ) {

                    peelComponent.unpeel( (Element)node );
                    addChild( peelComponent );
                }
            }
        }
    }

    public void addChild( PeelComponent child) {

        children.add( child);
        getComponent().add( child.getComponent(), child.getComponent().getBounds() );
         child.setParent( this );
         child.setRoot( getRoot() );
        if ( getRoot() != null )
            getRoot().addToMap( child );
    }

    public void addChild( int index, PeelComponent child ) {

        children.add( index, child);
        getComponent().add( index, child.getComponent(), child.getComponent().getBounds() );
        child.setParent( this );
        child.setRoot( getRoot() );
        if ( getRoot() != null )
            getRoot().addToMap( child );
    }

    public void removeChild( PeelComponent peelComponent ) {
                
        children.remove( peelComponent );
        getComponent().remove(peelComponent.getComponent());
        peelComponent.setParent( null );
        peelComponent.setRoot( null );

        if ( getRoot() != null ) {
            getRoot().removeFromMap( peelComponent );
        }
    }

    public void removeAllChildren() {

        List< PeelComponent > children_copy = new LinkedList< PeelComponent >( getChildren() );

        for ( PeelComponent component : children_copy ) {
            removeChild( component );
        }
    }

    public List< PeelComponent > getChildren() {
        return children;
    }

    public int getChildIndex( PeelComponent child ) {
        return getChildren().indexOf( child );
    }

    public Set< PeelComponent > getDescendants() {

        Set< PeelComponent > descendants = new HashSet< PeelComponent >();

        for ( PeelComponent child : getChildren() ) {

            if ( child instanceof PeelContainer )
                descendants.addAll( ((PeelContainer)child).getDescendants() );

            descendants.add( child );
        }

        return descendants;
    }

    public int getComponentCount() {
        return children.size();
    }

    public synchronized PeelComponent getChild(int index) {
        return children.get(index);
    }

    protected Element getPeeledInfo( Element element ) {
        super.getPeeledInfo( element );

        element.appendChild( getLayoutInfo( element ) );

        return element;

    }

    protected void unpeelInfo( Element element ) {
        super.unpeelInfo( element );

        Element layout_element = (Element)element.getElementsByTagName( Symbol.Node.layout ).item(0);
        String layout_name = layout_element.getAttribute( Symbol.Attr.type );

        BLayoutManager layout_manager = null;

        if ( layout_name.equals( Symbol.Attr.Layout.absolute ) ) {
            layout_manager = new NewAbsoluteLayout();
        }
        if ( layout_name.equals( Symbol.Attr.Layout.border ) ) {
            layout_manager = new BorderLayout();
        }
        if ( layout_name.equals( Symbol.Attr.Layout.group_hstretch ) ) layout_manager = GroupLayout.makeHStretch();
        if ( layout_name.equals( Symbol.Attr.Layout.group_vstretch ) ) layout_manager = GroupLayout.makeVStretch();

        if ( layout_name.equals( Symbol.Attr.Layout.table ) ) {
            layout_manager = null;
        }
        if ( layout_name.equals( Symbol.Attr.Layout.none ) ) layout_manager = null;


        if ( layout_manager != null )
            ((BContainer)getComponent()).setLayoutManager( layout_manager );
    }

    /* Look at the layout this container has and create an element for it */
    private Element getLayoutInfo( Element element ) {

        Element layout_element = element.getOwnerDocument().createElement( Symbol.Node.layout );

        BLayoutManager layout_manager = ((BContainer)component).getLayoutManager();

        String layout_type = Symbol.Attr.Layout.none;
        if ( layout_manager instanceof NewAbsoluteLayout) layout_type = Symbol.Attr.Layout.absolute;
        if ( layout_manager instanceof BorderLayout ) layout_type = Symbol.Attr.Layout.border;
        if ( layout_manager instanceof HGroupLayout ) layout_type = Symbol.Attr.Layout.group_hstretch;
        if ( layout_manager instanceof VGroupLayout) layout_type = Symbol.Attr.Layout.group_vstretch;
        if ( layout_manager instanceof TableLayout ) layout_type = Symbol.Attr.Layout.table;

        layout_element.setAttribute( Symbol.Attr.type, layout_type );

        return layout_element;
    }

    public BContainer getComponent() {
        return (BContainer)component;
    }
}
